package org.peipito.prs;


import org.peipito.prs.engine.GameEngine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceView;

// TODO Rename the class for something like ComputerInput.
public class RPSBlack {

	private static final String TAG = RPSBlack.class.getSimpleName();
	
	/**
	 * 0 is for rock.
	 * 1 is for paper.
	 * 2 is for scissor.
	 */
	private Bitmap [] blackRPS;
	
	/**
	 * contain the game logic.
	 */
	private GameEngine gameEngine;
	
	
	private Rect sourceRect;
	private Rect computerRect;
	
	public RPSBlack(Bitmap [] blackPRS, SurfaceView surfaceView){
		this.blackRPS = blackPRS;
		
		sourceRect = new Rect(0,0,blackPRS[0].getWidth(),blackPRS[0].getHeight());
		
	}
	
	public void init(){
		
	}
	
	public void update(long gameTime){
		
	}
	
	private int previous = 0;
	private boolean nextPic = true;
	private long time = 0;
	
	public void draw(Canvas canvas, SurfaceView surfaceView){

		computerRect = new Rect(0,										// left 
				0, 														// top
				surfaceView.getWidth(), 								// right
				surfaceView.getHeight() / 2);							// bottom
		
		if(GameEngine.gamePhase == GameEngine.COUNTDOWN){
			
			if(!nextPic){
				canvas.drawBitmap(blackRPS[previous], 0, 0, null);
//				canvas.drawBitmap(blackRPS[previous], sourceRect, computerRect, null);
				if( ( System.currentTimeMillis() - time ) > 250 )
					nextPic = true;
				return;
			}
			
			double d = Math.random() * 10;
			if(d<3.3){
				previous = 0;
			}else if (d < 6.6){
				previous = 1;
			}else {
				previous = 2;
			}
//			canvas.drawBitmap(blackRPS[previous], sourceRect, computerRect, null);
			canvas.drawBitmap(blackRPS[previous], 0, 0, null);
			nextPic = false;
			time = System.currentTimeMillis();
		}
		
		if(GameEngine.gamePhase == GameEngine.RESULT){
			
			int computerChoice = gameEngine.getComputerChoice();

			if(computerChoice == 0){
//				canvas.drawBitmap(blackRPS[0], sourceRect, computerRect, null);
				canvas.drawBitmap(blackRPS[0], 0, 0, null);
			} else if (computerChoice == 1){
//				canvas.drawBitmap(blackRPS[1], sourceRect, computerRect, null);
				canvas.drawBitmap(blackRPS[previous], 0, 0, null);
			} else {
//				canvas.drawBitmap(blackRPS[2], sourceRect, computerRect, null);
				canvas.drawBitmap(blackRPS[previous], 0, 0, null);
			}
		}
	}
	
	/**
	 * Doesn't do anything here as there isn't any interaction with the player.
	 * @param x
	 * @param y
	 * @param surfaceView
	 */
	public void handleActionDown(int x, int y, SurfaceView surfaceView){
		
	}
	
	public void setEngine(GameEngine gameEngine){
		this.gameEngine = gameEngine;
	}
	
}
